package com.dhl.prototypepattern.serialize;

import java.io.*;

/**
 * @title: SerializeTest
 * @Author danghailiang
 * @Date: 2022/7/28 22:09
 * @Version 1.0
 */
public class SerializeTest {

    public static void main(String[] args) {
        SerializeTom serializeTom = new SerializeTom(1L, "serializeTom", new SerializeYang(2L, "serializeYang"));
        System.out.println(serializeTom);
        System.out.println(SerializeTest.getSerializeTom(serializeTom));
        System.out.println(SerializeTest.getSerializeTom(serializeTom));
        System.out.println(SerializeTest.getSerializeTom(serializeTom));

    }
    public static SerializeTom getSerializeTom(SerializeTom serializeTom){
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("serializeTom.txt"));
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("serializeTom.txt"))
                ) {
            oos.writeObject(serializeTom);
            serializeTom =  (SerializeTom)ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try(
//                ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                ObjectOutputStream oos = new ObjectOutputStream(baos);
//                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
//                ObjectInputStream ois = new ObjectInputStream(bais)
//                ){
//            oos.writeObject(serializeTom);
//            serializeTom =  (SerializeTom)ois.readObject();
//
//
//        }
//        catch (Exception e){
////            e.printStackTrace();
//        }
        return serializeTom;
    }
}
